package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyXinYonDai;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 安心分期-信用秒借
 *
 * @author WJB
 */
@ApiPlan(productId = "406", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class ApiOrgPhoneMaskXinYonDai implements ApiOrgPhoneMaskConvertHandler {
    private static final String CODE = "20250812144810";
    private static final String KEY = "4d0ed54da6281523";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        Map<Object, Object> phone = new LinkedHashMap<>();
        phone.put("phoneMask", apiDataExchangeParameter.getPhoneMask());

        Map<Object, Object> newMap = new LinkedHashMap<>();
        AES aes = AES.createEcb(KEY.getBytes());
        newMap.put("data", aes.encryptToBase64Str(JsonUtils.toJson(phone)));
        return newMap;
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        JSONArray phoneList = jsonObject.getJSONArray("phoneList");
        RespOrgPhoneMaskDataExchange respOrgPhoneMaskDataExchange = RespOrgPhoneMaskDataExchange.ofSuc();
        List<String> md5 = new ArrayList<>();
        for (int i = 0; i < phoneList.size(); i++) {
            String phoneMd5 = phoneList.getString(i);
            md5.add(phoneMd5);
        }
        respOrgPhoneMaskDataExchange.setListPhoneMd5(md5);
        return respOrgPhoneMaskDataExchange;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyXinYonDai data = new OrgApplyXinYonDai();
        data.setPhone(apiApplyParameter.getPhone());
        //  客户姓名
        data.setName(apiApplyParameter.getUserName());
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  年龄(如：39)
        data.setAge(apiFieldConverter.getAgeInt());
        data.setCode(CODE);
        //  城市
        data.setCity(apiApplyParameter.getCity());
        data.setCityCode(apiApplyParameter.getAreaCode());
        //  性别
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 0)
                .when(apiApplyParameter.getSex(), "1", 1)
                .end(0);
        data.setSex(sex);
        //  借款金额
        Integer loanAmount = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 30000)
                .when(apiApplyParameter.getLoanAmount(), "2", 50000)
                .when(apiApplyParameter.getLoanAmount(), "3", 100000)
                .when(apiApplyParameter.getLoanAmount(), "4", 200000)
                .end(30000);
        data.setLoanAmount(loanAmount);
        //  借款期限
        Integer loanTerm = ChooseEq.create()
                .when(apiApplyParameter.getLoanTerm(), "1", 1)
                .when(apiApplyParameter.getLoanTerm(), "2", 1)
                .when(apiApplyParameter.getLoanTerm(), "3", 3)
                .when(apiApplyParameter.getLoanTerm(), "4", 4)
                .when(apiApplyParameter.getLoanTerm(), "5", 5)
                .when(apiApplyParameter.getLoanTerm(), "6", 5)
                .end(1);
        data.setLoanTerm(loanTerm);
        //  社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 3)
                .when(apiApplyParameter.getSocialSecurity(), "2", 4)
                .when(apiApplyParameter.getSocialSecurity(), "3", 5)
                .when(apiApplyParameter.getSocialSecurity(), "4", 4)
                .end(3);
        data.setSalary(socialSecurity);
        //  公积金
        Integer providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 3)
                .when(apiApplyParameter.getProvidentFund(), "2", 4)
                .when(apiApplyParameter.getProvidentFund(), "3", 5)
                .when(apiApplyParameter.getProvidentFund(), "4", 4)
                .end(3);
        data.setAccumulation(providentFund);
        //  车
        Integer CarInfo = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 3)
                .when(apiApplyParameter.getCarInfo(), "2", 4)
                .when(apiApplyParameter.getCarInfo(), "3", 4)
                .when(apiApplyParameter.getCarInfo(), "4", 4)
                .end(3);
        data.setCar(CarInfo);
        //  房
        Integer RealEstateInfo = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 3)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 4)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 4)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 4)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 4)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 4)
                .end(3);
        data.setHouse(RealEstateInfo);
        //  保单
        Integer insurancePolicy = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 3)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 4)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 5)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 4)
                .end(3);
        data.setInsurance(insurancePolicy);
        //  芝麻信用
        Integer sesameCreditChoose = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 2)
                .when(apiApplyParameter.getSesameCredit(), "2", 3)
                .when(apiApplyParameter.getSesameCredit(), "3", 4)
                .when(apiApplyParameter.getSesameCredit(), "4", 5)
                .end(1);
        data.setSesameIntegral(sesameCreditChoose);
        //  职业
        Integer career = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", 1)
                .when(apiApplyParameter.getCareer(), "2", 5)
                .when(apiApplyParameter.getCareer(), "3", 3)
                .when(apiApplyParameter.getCareer(), "4", 2)
                .when(apiApplyParameter.getCareer(), "5", 4)
                .end(1);
        data.setWork(career);
        //  信用卡
        Integer creditLimit = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", 1)
                .when(apiApplyParameter.getCreditLimit(), "2", 2)
                .when(apiApplyParameter.getCreditLimit(), "3", 2)
                .when(apiApplyParameter.getCreditLimit(), "4", 2)
                .when(apiApplyParameter.getCreditLimit(), "5", 2)
                .when(apiApplyParameter.getCreditLimit(), "6", 2)
                .end(1);
        data.setCreditCard(creditLimit);
        //  学历
        Integer text = ChooseEq.create()
                .when(apiApplyParameter.getEducationAttainment(), "1", 1)
                .when(apiApplyParameter.getEducationAttainment(), "2", 2)
                .when(apiApplyParameter.getEducationAttainment(), "3", 2)
                .when(apiApplyParameter.getEducationAttainment(), "4", 3)
                .when(apiApplyParameter.getEducationAttainment(), "5", 4)
                .when(apiApplyParameter.getEducationAttainment(), "6", 5)
                .end(1);
        data.setEducation(text);
        //  月收入
        Integer monthlySalary = ChooseEq.create()
                .when(apiApplyParameter.getMonthlySalary(), "1", 1)
                .when(apiApplyParameter.getMonthlySalary(), "2", 2)
                .when(apiApplyParameter.getMonthlySalary(), "3", 3)
                .when(apiApplyParameter.getMonthlySalary(), "4", 4)
                .when(apiApplyParameter.getMonthlySalary(), "5", 4)
                .end(1);
        data.setMonthlyIncome(monthlySalary);
        //  代发
        Integer salaryPayMethod = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "2", 2)
                .when(apiApplyParameter.getSalaryPayMethod(), "3", 3)
                .end(1);
        data.setIncomeForm(salaryPayMethod);
        //  当前单位工龄
        Integer workingYears = ChooseEq.create()
                .when(apiApplyParameter.getYearsService(), "1", 1)
                .when(apiApplyParameter.getYearsService(), "2", 2)
                .when(apiApplyParameter.getYearsService(), "3", 3)
                .end(1);
        data.setLengthOfService(workingYears);
        //  逾期
        Integer overdue = ChooseEq.create()
                .when(apiApplyParameter.getOverdue(), "1", 2)
                .when(apiApplyParameter.getOverdue(), "2", 3)
                .when(apiApplyParameter.getOverdue(), "3", 2)
                .end(2);
        data.setCreditInvestigation(overdue);
        //  花呗
        Integer huaBei = ChooseEq.create()
                .when(apiApplyParameter.getHbLimit(), "1", 1)
                .when(apiApplyParameter.getHbLimit(), "2", 2)
                .when(apiApplyParameter.getHbLimit(), "3", 3)
                .when(apiApplyParameter.getHbLimit(), "4", 4)
                .when(apiApplyParameter.getHbLimit(), "5", 4)
                .end(1);
        data.setHuaBeiQuota(huaBei);
        //  白条
        Integer btLimit = ChooseEq.create()
                .when(apiApplyParameter.getBtLimit(), "1", 1)
                .when(apiApplyParameter.getBtLimit(), "2", 2)
                .when(apiApplyParameter.getBtLimit(), "3", 3)
                .when(apiApplyParameter.getBtLimit(), "4", 4)
                .when(apiApplyParameter.getBtLimit(), "5", 4)
                .end(1);
        data.setBaiTiaoQuota(btLimit);
        Map<Object, Object> newMap = new LinkedHashMap<>();
        AES aes = AES.createEcb(KEY.getBytes());
        newMap.put("data", aes.encryptToBase64Str(JsonUtils.toJson(data)));
        return newMap;
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
